<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Comment;
use App\Discuss;
use App\Parsedown;
use App\Permission;
use App\Task;
use App\User;
use App\Utils;
use App\Studyfav;
use App\Review;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;

class ReviewController extends Controller
{

    public function randomPickVote(Request $request) {
        return $this->randomVote($request, function () {
            return $comments = DB::select('select * from comments where jinghua_vote > 0 and `delete` = 0');
        }, 3);
    }

    public function randomDeleteVote(Request $request) {
       return $this->randomVote($request, function () {
           return $comments = DB::select('select * from comments where delete_vote > 0 and `delete` = 0');
       }, 2);
    }

    public function randomVote(Request $request, $comments_getting, $review_type)
    {
        $comments = $comments_getting();
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', $review_type)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        foreach ($comments as $comment) {
            if (!array_key_exists($comment->id, $review_set)) {
                return redirect()->action('ReviewController@show',
                    [
                        'dis_id' => Utils::id2url($comment->taskid),
                        'comment_id' => Utils::id2url($comment->id),
                        'type' => $review_type
                    ]
                );
            }
        }
        return View::make('dis.no_review');
    }

    public function index(Request $request)
    {
        $type = "review";
        return View::make('dis.review_queue_index')->with(compact('type'));
    }

    public function randomStudy(Request $request)
    {
        return $this->randomComment($request, 1, function ($dis) {
            if ($dis == null) {
                return false;
            }
            if (strpos($dis->title, '的学习资料') === false) {
                return false;
            }
            return true;
        }, 0);
    }

    public function randomNormalComment(Request $request)
    {
        return $this->randomComment($request, 1, function ($dis) {
            if ($dis == null) {
                return false;
            }
            if (strpos($dis->title, '的学习资料') !== false) {
                return false;
            }
            return true;
        }, 1);
    }

    public function randomComment(Request $request, $comment_type, $dis_checking, $review_type)
    {
        $comments = DB::select('select * from comments where type=' . $comment_type . ' and review=0 and `delete`=0');
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', 0)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        foreach ($comments as $comment) {
            $dis = Discuss::find($comment->taskid);
            if ($dis_checking($dis) === false) {
                continue;
            }
            if (!array_key_exists($comment->id, $review_set)) {
                return redirect()->action('ReviewController@show',
                    [
                        'dis_id' => Utils::id2url($comment->taskid),
                        'comment_id' => Utils::id2url($comment->id),
                        'type' => $review_type
                    ]
                );
            }
        }
        return View::make('dis.no_review');
    }

    public function show(Request $request, $dis_id, $comment_id, $review_type)
    {
        if (!Permission::user_can_review()) {
            return View::make('errors.404');
        }
        $type = "review";
        $dis_id = Utils::url2id($dis_id);
        if ($comment_id != "0") $comment_id = Utils::url2id($comment_id);
        $dis = Discuss::findOrFail($dis_id);
        $tasks = Task::whereTitle($dis->tag)->get();
        $dis->vote += 1;
        $parser = new ParseDown();
        if ($comment_id == 0) {
            $comments = Comment::whereTypeAndTaskidAndDelete(1, $dis_id, 0)->orderBy("vote", "desc")->paginate(10);
            $dis->ansnum = $comments->total();
            $dis->save();
        } else {
            $comments = Comment::whereId($comment_id)->paginate(10);
        }

        $dis->content = $parser->text($dis->content);
        $dis->user = User::find($dis->userid);
        foreach ($comments as $c) {
            $c->user = User::find($c->userid);
            $c->comment = $parser->text($c->comment);
            $respons = Comment::whereTypeAndTaskidAndDelete(3, $c->id, 0)->orderBy("created_at", "asc")->get();
            foreach ($respons as $r) {
                $r->user = User::find($r->userid);
                if ($r->ruserid != 0) $r->ruser = User::find($r->ruserid);
            }
            $c->respons = $respons;
            $c->fav = 1;
            if (Auth::check()) {
                $fav = Studyfav::whereUseridAndStudyidAndType(Auth::user()->id, $c->id, 1)->orderBy('created_at', 'desc')->get()->first();
                if ($fav != null) $c->fav = 0;
            }
        }

        return View::make("dis.p")->with(compact("dis"))
            ->with(compact("comments"))
            ->with(compact("tasks"))
            ->with(compact('type'))
            ->with(compact('review_type'));
    }

    public function is_reviewed($comment_id) {
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('comment_id', $comment_id)->get();
        if (count($reviews) > 0) {
            return true;
        } else {
            return false;
        }
    }


    public function approve(Request $request, $comment_id, $type)
    {
        if (!Permission::user_can_review()) {
            return View::make('errors.404');
        }

        $comment_id = Utils::url2id($comment_id);
        $comment = Comment::find($comment_id);
        if ($this->is_reviewed($comment_id)) {
            return $this->returnByType($request, $type);
        }
        $comment->approval += 1;
        if ($comment->approval >= 3) {
            $comment->review = 1;
        }
        $comment->save();
        $review = array();
        $review['reviewer_user_id'] = Auth::user()->id;
        $review['comment_id'] = $comment_id;
        $review['review_result'] = 1;
        Review::create($review);
        return $this->returnByType($request, $type);
    }

    public function skip(Request $request, $comment_id, $type)
    {
        if (!Permission::user_can_review()) {
            return View::make('errors.404');
        }
        $comment_id = Utils::url2id($comment_id);
        if ($this->is_reviewed($comment_id)) {
            return $this->returnByType($request, $type);
        }
        $review = array();
        $review['reviewer_user_id'] = Auth::user()->id;
        $review['comment_id'] = $comment_id;
        $review['review_result'] = 0;
        $review['type'] = $type;
        Review::create($review);
        return $this->returnByType($request, $type);
    }

    public function voteDeleteComment(Request $request, $action, $comment_id, $type) {
        if (!Permission::user_can_review()) {
            return View::make('errors.404');
        }
        $comment_id = Utils::url2id($comment_id);
        $comment = Comment::find($comment_id);
        if ($this->is_reviewed($comment_id)) {
            return $this->returnByType($request, $type);
        }
        if ($action === 'approve') {
            $score = 1;
        } else if ($action === 'reject') {
            $score = -1;
        } else {
            return View::make('errors.404');
        }
        $comment->delete_vote += $score;
        if ($comment->delete_vote >= 3) {
            $comment->delete = 1;
            $comment->delete_vote = 0;
        }
        $comment->save();
        $review = array();
        $review['reviewer_user_id'] = Auth::user()->id;
        $review['comment_id'] = $comment_id;
        $review['review_result'] = 1;
        $review['type'] = 2;
        Review::create($review);
        return $this->returnByType($request, $type, $comment->taskid);
    }

    public function voteJingHuaComment(Request $request, $action, $comment_id, $type) {
        if (!Permission::user_can_review()) {
            return View::make('errors.404');
        }
        $comment_id = Utils::url2id($comment_id);
        $comment = Comment::find($comment_id);
        if ($this->is_reviewed($comment_id)) {
            return $this->returnByType($request, $type);
        }
        if ($action === 'approve') {
            $score = 1;
        } else if ($action === 'reject') {
            $score = -1;
        } else {
            return View::make('errors.404');
        }
        $comment->jinghua_vote += $score;
        if ($comment->jinghua_vote >= 3) {
            $comment->jinghua = 1;
            $comment->jinghua_vote = 0;
        }
        $comment->save();
        $review = array();
        $review['reviewer_user_id'] = Auth::user()->id;
        $review['comment_id'] = $comment_id;
        $review['review_result'] = 1;
        $review['type'] = 3;
        Review::create($review);
        return $this->returnByType($request, $type, $comment->taskid);
    }

    public function returnByType(Request $request, $type, $dis_id = 0)
    {
        switch ($type) {
            case 0:
                return $this->randomStudy($request);
            case 1:
                return $this->randomNormalComment($request);
            case 2:
                return $this->randomDeleteVote($request);
            case 3:
                return $this->randomPickVote($request);
            case 100:
                return Redirect::to("/q/" . Utils::id2url($dis_id));
        }
        return View::make('errors.404');
    }
}
